package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import com.netflix.ribbon.proxy.annotation.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    //查询 规格组
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid) {
        List<SpecGroup> list = this.specificationService.querySpecGroups(cid);
        if (list == null && list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    //查询 规格参数
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParam(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching,
            @RequestParam(value = "generic", required = false) Boolean generic) {
        List<SpecParam> list = this.specificationService.querySpecParam(gid,cid,searching,generic);
        if (list == null && list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    //新增 规格分组
    @PostMapping("group")
    public ResponseEntity<Void> addSpecGroup(@RequestParam(value = "name") String name, @RequestParam(value = "cid") Long cid) {
        int i = this.specificationService.addSpecGroup(name, cid);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.CREATED);
        }
        return null;
    }

    //修改 规格分组
    @PutMapping("group")
    public ResponseEntity<Void> editaSpecGroup(@RequestParam("id") Long id, @RequestParam(value = "name") String name, @RequestParam(value = "cid") Long cid) {
        int i = this.specificationService.editSpecGroup(id, name, cid);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.ACCEPTED);
        }
        return null;
    }

    //删除 规格分组  若直接删除规格分组，则其下面的规格参数也应一并删除，规格参数是根据gid进行删除
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> delSpecGroup(@PathVariable("id") Long id) {
        int i = this.specificationService.delSpecGroup(id);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.ACCEPTED);
        }
        return null;
    }

    //新增 规格参数
    @PostMapping("param")
    public ResponseEntity<Void> addSpecParam(
            @RequestParam("cid") Long cid,
            @RequestParam("groupId") Long groupId,
            @RequestParam("name") String name,
            @RequestParam(value = "numeric", required = false) Boolean numeric,
            @RequestParam(value = "unit", required = false) String unit,
            @RequestParam("generic") Boolean generic,
            @RequestParam("searching") Boolean searching,
            @RequestParam("segments") String segments
    ) {
        int i = this.specificationService.addSpecParam(cid, groupId, name, numeric, unit, generic, searching, segments);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.ACCEPTED);
        }
        return null;
    }

    //修改  规格参数
    @PutMapping("param")
    public ResponseEntity<Void> editSpecParam(SpecParam specParam) {
        int i = this.specificationService.editSpecParam(specParam);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.ACCEPTED);
        }
        return null;
    }

    //删除规格参数
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> editSpecParam(@PathVariable("id") Long id) {
        System.out.println("id = " + id);
        int i = this.specificationService.delSpecParam(id);
        if (i > 0) {
            return new ResponseEntity<>(HttpStatus.ACCEPTED);
        }
        return null;
    }

    //查询规格参数组及组内参数
    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecsByCid(@PathVariable("cid") Long cid) {
        List<SpecGroup> list = this.specificationService.querySpecsByCid(cid);
        if (list == null || list.size() <= 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }
}
